import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Base64;

public class Base64FileEncoder {

    public static void main(String[] args) {
        String inputFilePath = "src/main/resources/sensi_word/sensitive_words_lines.txt"; // 需要加密的文件路径
        String outputFilePath = "src/main/resources/sensi_word/sensitive_words_lines_port.txt"; // 生成的新文件路径

        try {
            encodeFileToBase64(inputFilePath, outputFilePath);
            System.out.println("文件已成功编码并保存到: " + outputFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void encodeFileToBase64(String inputFilePath, String outputFilePath) throws IOException {
        // 读取文件内容
        try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
             BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {

            String line;
            while ((line = reader.readLine()) != null) {
                // 进行 Base64 编码
                String encodedLine = Base64.getEncoder().encodeToString(line.getBytes());
                // 将编码后的内容写入新文件
                writer.write(encodedLine);
                writer.newLine(); // 保持原文件中的行结构
            }
        }
    }
}
